package com.yijiajiao.finance.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yijiajiao.finance.bean.*;
import com.yijiajiao.finance.bean.alipay.AlipayCore;
import com.yijiajiao.finance.bean.alipay.config.AlipayConfig;
import com.yijiajiao.finance.bean.alipay.sign.RSA;
import com.yijiajiao.finance.mappers.BatchPayDetailMapper;
import com.yijiajiao.finance.mappers.BatchPaymentMapper;
import com.yijiajiao.finance.mappers.TeacherMoneyMapper;
import com.yijiajiao.finance.service.BatchPayService;
import com.yijiajiao.finance.util.Config;
import com.yijiajiao.finance.util.DateUtil;
import com.yijiajiao.finance.util.StringUtil;
import javafx.scene.control.Pagination;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;

/**
 * @AUTHOR zhaoming@eduspace
 * @CREATE 2017-05-31-17:20
 */
@Service("batchPayService")
public class BatchPayServiceImpl implements BatchPayService {

    private static Logger log = LoggerFactory.getLogger(BatchPayServiceImpl.class);

    @Autowired
    private BatchPayDetailMapper batchPayDetailMapper;
    @Autowired
    private BatchPaymentMapper batchPaymentMapper;

    private TeacherMoneyMapper teacherMoneyMapper;

    @Override
    public ResultBean batchPay(List<BatchPayDetail> batchPayDetails) {
        ResultBean resultBean = new ResultBean();
        if(batchPayDetails.size()>1000){//每个批次最多转款1000笔
            return ResultBean.getFailResult(SystemStatus.BATCH_NUM);
        }
        
        StringBuilder detailData = new StringBuilder();
        String batch_no = DateUtil.getNowTime("yyyyMMddhhmmss")+ StringUtil.getRandomCharNum(2);//获得唯一批次号
        BigDecimal batch_fee = BigDecimal.valueOf(0.00);
        int running = 1001;
        for(BatchPayDetail bpd :batchPayDetails){
            bpd.setBatchNo(batch_no);
            bpd.setRunningNo(batch_no+running);//生成流水号
            if(detailData.length()>0){
                detailData.append("|"+bpd.getPayString());
            }else{
                detailData.append(bpd.getPayString());
            }
            batch_fee=batch_fee.add(bpd.getProceedsFee());
            running++;
        }
        Map<String, String> sParaTemp = new HashMap<>();
        sParaTemp.put("account_name", AlipayConfig.account_name);
        sParaTemp.put("batch_fee", batch_fee+"");
        sParaTemp.put("batch_no",batch_no);
        sParaTemp.put("batch_num",batchPayDetails.size()+"");
        sParaTemp.put("detailData", detailData.toString());
        sParaTemp.put("email", AlipayConfig.email);
        sParaTemp.put("partner", AlipayConfig.partner);
        sParaTemp.put("pay_date", DateUtil.getNowTime("yyyyMMdd"));
        sParaTemp.put("_input_charset", AlipayConfig.input_charset);
        sParaTemp.put("notify_url", Config.getString("notify_url"));
        sParaTemp.put("service",Config.getString("service"));
        String content = AlipayCore.createLinkString(sParaTemp);
        String sign_content = RSA.sign(content, AlipayConfig.private_key, AlipayConfig.input_charset);
        String result = Config.getString("web_pay_url")+content+"&sign="+ URLEncoder.encode( sign_content )+"&sign_type="+ AlipayConfig.sign_type;
        log.info("result = "+result);

        //保存支付记录
        BatchPayment batchPayment = new BatchPayment();
        batchPayment.setBatchNo(batch_no);
        batchPayment.setBatchFee(batch_fee);
        batchPayment.setBatchNum(batchPayDetails.size());
        batchPayment.setPayDate(new Date());
        batchPayment.setEmail(sParaTemp.get("email"));
        batchPayment.setPayUserName(sParaTemp.get("account_name"));
        batchPaymentMapper.insertBatchPayment(batchPayment);
        log.info("batchPayment保存成功！"+batchPayment);
        DateUtil du = new DateUtil();
        Date beginMonth = du.calcBeginMonth(DateUtil.getNowTime());
        //上个月第一天
        String lasb = DateUtil.calcDate(beginMonth,DateUtil.YYYY_MM_DD, 2, -1);
        for(BatchPayDetail bpd :batchPayDetails){
            bpd.setStartTime(lasb);
            batchPayDetailMapper.updateNumber(bpd);
        }
        log.info("本批次发放工资的人数=="+batchPayDetails.size());
        return ResultBean.getSucResult(result);
    }

    @Override
    public Pagination querySettleInfo(String queryDate, String phone) {
        return null;
    }

    @Override
    public ResultBean querySettleInfoByDate(BatchPayDetailQuery batchPayDetailQuery) {

        return null;

    }

    @Override
    public int getAndSaveSalary() {
        return 0;

    }


}
